import request from '@/utils/request';

export type ActivityStatus = 'upcoming' | 'ongoing' | 'finished';

export interface ActivityListParams {
  page: number;
  pageSize: number;
  type?: ActivityStatus;
}

export interface Activity {
  id: number;
  title: string;
  description: string;
  status: string;
  start_time: string;
  end_time: string;
  type: string;
  location: string;
  cover_image: string;
  route_description: string;
  max_participants: number;
  requirements: string;
  current_participants: number;
  creator_id: number;
  created_at: string;
  updated_at: string;
}

export enum FeeMode {
  FREE = 'FREE',
  PAID = 'PAID',
  DEPOSIT = 'DEPOSIT'
}

export enum DifficultyLevel {
  EASY = 'EASY',
  MEDIUM = 'MEDIUM',
  HARD = 'HARD'
}

export interface CreateActivityDTO {
  title: string;
  description: string;
  type: string;
  startLocation: string;
  endLocation: string;
  startTime: string;
  endTime: string;
  routeDescription: string;
  maxParticipants: number;
  minParticipants: number;
  difficultyLevel: DifficultyLevel;
  requirements: string;
  equipmentRequired: string;
  feeMode: FeeMode;
  feeAmount?: number;
  depositAmount?: number;
  feeDescription: string;
  cover: string;
  tag?: string;
}

export interface ActivityListResult {
  list: Activity[];  // 改为 list
  total: number;
  page: number;
  pageSize: number;
  totalPages: number;
}

export const activityApi = {
  // 获取活动列表
  getActivityList(params: ActivityListParams) {
    return request.get('/activity/list', {
      params,
      // 添加错误处理选项
      validateStatus: (status) => status >= 200 && status < 300
    })
  },

  // 获取活动详情
  getActivityDetail: (id: number) => {
    return request.get<Activity>(`/activity/detail/${id}`)
  },

  // 参加活动
  joinActivity: (id: number) => {
    return request.post(`/activity/join/${id}`)
  },

  // 检查活动收藏状态
  checkFavoriteStatus: (id: number | string) => {
    return request.get<{ isFavorite: boolean }>(`/activity/${id}/favorite/check`)
  },

  // 检查活动报名状态
  checkJoinStatus: (id: number | string) => {
    return request.get<{ isJoined: boolean }>(`/activity/${id}/join/check`)
  },

  // 收藏活动
  favoriteActivity: (id: number | string) => {
    return request.post(`/activity/${id}/favorite`)
  },

  // 取消收藏活动
  unfavoriteActivity: (id: number | string) => {
    return request.delete(`/activity/${id}/favorite`)
  },

  // 上传活动封面图片
  uploadImage: (file: File) => {
    const formData = new FormData();
    formData.append('image', file);
    return request.post<{ url: string }>('/activity/upload-image', formData, {
      headers: {
        'Content-Type': 'multipart/form-data',
      },
    });
  },

  // 创建活动
  createActivity: (data: CreateActivityDTO) => {
    return request.post<Activity>('/activity/create', data)
  },

  // 上传活动图片
  uploadActivityImage: (formData: FormData) => {
    return request.post<{data: {url: string}}>('/activity/upload-image', formData, {
      headers: {
        'Content-Type': 'multipart/form-data'
      }
    }).then(response => {
      // 确保返回正确的数据结构
      console.log('API响应:', response)
      return response.data || response
    })
  },

  // 获取用户参与的活动
  getUserParticipatedActivities: (page: number = 1, pageSize: number = 10) => {
    return request.get<ActivityListResult>('/activity/user/participated', {
      params: { page, pageSize }
    });
  },

  // 获取用户创建的活动
  getUserCreatedActivities: (page: number = 1, pageSize: number = 10) => {
    return request.get<ActivityListResult>('/activity/user/created', {
      params: { page, pageSize }
    });
  },
}
